using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using InfoMan;
    
namespace Client
{
    /// <summary>
    /// Summary description for ApplicationClass.
    /// </summary>
    class ApplicationClass
    {
        int dataCollectionLength;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // Create a TCP channel.
            TcpChannel theChannel = new TcpChannel();

            /* Register the channel so that this client can
            connect to the server. */
        //    ChannelServices.RegisterChannel(theChannel);
            ChannelServices.RegisterChannel(theChannel,false);


            // Activate the server object. 
            IInformationManager infoMan = 
                (IInformationManager)Activator.GetObject(
                    typeof(IInformationManager),
                    "tcp://localhost:8085/InfoMan");

           
            
            try
            {
                string tempString2;
                tempString2 = infoMan.getAcknowledgement();
                Console.WriteLine(tempString2);

                Console.WriteLine("\nSetting the interger to 40 \n"); 
                infoMan.setInt(40);


                Console.WriteLine("\nThe integer value is: {0} \n",infoMan.getInt());


            }
            catch (Exception)
            {
                ServerAccessError();
                
            }
           


            // If the remote object was activated...
            if (infoMan != null)
            {
                string tempString;
                int dataCollectionLength;       
      
                try
                {
                    dataCollectionLength = 
                        infoMan.DataCollectionLength();
               //     tempString2 = infoMan.getAcknowledgement();
                //    Console.WriteLine(tempString2);

                }
                catch (Exception)
                {
                    ServerAccessError();
                    dataCollectionLength = 0;
                }

                bool errorOccurred = false;
                // For each item in the "database"
                for (int i=0; 
                     (i<dataCollectionLength) &&
                         (!errorOccurred);
                     i++)
                {
                    tempString = null; 
    
                    try
                    {
                        // Get the item.
                        tempString = infoMan.DataItem(i);
    
                        // Print it to the console.
                        System.Console.WriteLine(tempString);
                    }
                    catch (Exception)
                    {
                        ServerAccessError();
                        errorOccurred = true;
                    } 
                }
                System.Console.WriteLine("\nThe total size of the array is : " + dataCollectionLength);
            }
            // Else the remote object was not activated...
            else
            {
                // Print an error message.
                System.Console.WriteLine(
                    "Remote object could not be activated");
            }

            System.Console.WriteLine(
                "\nPaused. Press Enter to continue...");
            System.Console.Read();
        }

        static void ServerAccessError()
        {
            System.Console.WriteLine(
                "Error: Could not access the server");
        }
    }
}
